package com.lkd.http.controller;
import com.github.wxpay.plus.WXConfig;
import com.github.wxpay.plus.WxPayParam;
import com.github.wxpay.plus.WxPayTemplate;
import com.lkd.common.VMSystem;
import com.lkd.config.TopicConfig;
import com.lkd.emq.MqttProducer;
import com.lkd.entity.OrderEntity;
import com.lkd.service.OrderService;
import com.lkd.vo.PayVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

@RestController
@RequestMapping("/order")
@Slf4j
public class OrderController {

    @Autowired
    private OrderService orderService;

    @Autowired
    private WxPayTemplate wxPayTemplate;

    @Autowired
    private MqttProducer producer;
    @GetMapping("/test")
    public void test(){
        producer.send(TopicConfig.getVendoutTopic("1-1"),"ad");
    }

    @PostMapping("/requestPay")
    public Map requestPay(@RequestBody PayVO payVO){
        //1.创建订单数据库内容
        OrderEntity orderEntity =  orderService.createOrder(payVO);

        //2.对接微信支付接口创建支付二维码
        var param=new WxPayParam();       //封装支付请求对象调用支付
        param.setBody(orderEntity.getSkuName());//商品名称
        param.setOutTradeNo(orderEntity.getOrderNo());// 订单号
        param.setTotalFee(orderEntity.getAmount().intValue());//金额 分 单位
        param.setOpenid(orderEntity.getOpenId());//如果本地支付openid为""
        return wxPayTemplate.requestPay(param);
    }

    /**
     * 微信支付回调接口
     * @param request
     * @return
     */
    @RequestMapping("/payNotify")
    @ResponseBody
    public void payNotify(HttpServletRequest request, HttpServletResponse response){

        try {
            Map<String, String> result = wxPayTemplate.validPay(request.getInputStream());
            if("SUCCESS".equals( result.get("code") )){  //返回码成功
                String orderSn= result.get("order_sn");//获取订单号
                log.info("支付成功，修改订单状态和支付状态，订单号：{}",orderSn);
                //根据订单号获取订单数据
                OrderEntity order = orderService.getByOrderNo(orderSn);
                if(order == null){
                    log.error("订单数据不存在,订单号:" + orderSn);
                    response.setContentType("text/xml");
                    response.getWriter().write(WXConfig.RESULT);
                    return;
                }
                String transactionId = result.get("transaction_id");
                order.setThirdNo(transactionId);
                order.setStatus(VMSystem.ORDER_STATUS_PAYED);
                order.setPayStatus(VMSystem.PAY_STATUS_PAYED);
                //支付流水号
                orderService.updateById(order);

                //出货
                orderService.vendOut(order);
            }
            //给微信支付一个成功的响应
            response.setContentType("text/xml");
            response.getWriter().write(WXConfig.RESULT);
        }catch (Exception e){
            log.error("支付回调处理失败",e);
        }
    }

}
